<?php

/**
 * uniBoard API Style Template
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date$
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_template extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore			= NULL;
	
	/**
	 * Template ID
	 *
	 * @var integer
	 */
	
	public $template_id		= 0;
	
	/**
	 * Style ID
	 *
	 * @var integer
	 */
	
	public $style_id 			= 0;
	
	/**
	 * Style Name
	 *
	 * @var string
	 */
	
	public $style_name 		= NULL;
	
	/**
	 * Template group
	 *
	 * @var string
	 */
	
	public $template_group 	= '';
	
	/**
	 * Template Name
	 *
	 * @var string
	 */
	
	public $template_name		= '';
	
	/**
	 * Template code
	 *
	 * @var string
	 */
	
	public $template_code 		= '';
		
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store template id
		$this -> template_id = (integer) $target_id;
		
		// Autoload If Template ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT t.*, s.style_name
			FROM ' . DB_PREFIX . 'styles_templates t
			LEFT JOIN ' . DB_PREFIX . 'styles s ON t.style_id = s.style_id
			WHERE t.template_id = \'' . $this -> template_id . '\'
			LIMIT 1');
		
			// Template found
			if ( $tpl_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded			= true;
				
				$this -> style_id	 			= $tpl_result['style_id'];
				$this -> style_name	 			= $tpl_result['style_name'];
				$this -> template_group			= $tpl_result['template_group'];
				$this -> template_name 			= $tpl_result['template_name'];
				$this -> template_code			= $tpl_result['template_code'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_template_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> template_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'styles_templates');
			
				// Store new template id
				$this -> template_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'styles_templates', 'template_id = \'' . $this -> template_id . '\'');
			}
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data to DB
	 *
	 */
	
	function deleteData(){
		
		// Delete tpl from DB
		$this -> appCore -> db -> delete( 'styles_templates', 'template_id = \'' . $this -> template_id . '\'');
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set style_id
	 *
	 * @param integer $new_value
	 */
	
	function setStyleId( $new_value){
		
		// Store in object
		$this -> style_id = (int) $new_value;
		
		// Add to made changes
		$this -> changes['style_id'] = $this -> style_id;
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set template_group
	 *
	 * @param string $new_value
	 */
	
	function setTemplateGroup( $new_value){

		// New value is not empty?
		if ( isset( $new_value[0]))
		{	
			// Store in object
			$this -> template_group = $this -> appCore -> secureFileName( strtolower( $new_value));
			
			// Too long for mysql?
			if ( mb_strlen( $new_value) > 255)
			{
				$this -> setError( $this -> appCore -> lang['acp_template_group_too_long']);	
			}
			else
			{
				// Add to made changes
				$this -> changes['template_group'] = $this -> appCore -> db -> strEscape( $this -> template_group);
			}
		}
		else
		{
			$this -> setError( $this -> appCore -> lang['acp_template_group_empty']);	
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set template_name
	 *
	 * @param string $new_value
	 */
	
	function setTemplateName( $new_value){
		
		// New value is not empty?
		if ( isset( $new_value[0]))
		{	
			// Store in object
			$this -> template_name = $this -> appCore -> secureFileName( strtolower( $new_value));
			
			// Too long for mysql?
			if ( mb_strlen( $new_value) > 255)
			{
				$this -> setError( $this -> appCore -> lang['acp_template_name_too_long']);	
			}
			else
			{
				// Name taken?
				if ( $this -> appCore -> db -> count( 'styles_templates', 'style_id = \'' . $this -> style_id . '\' AND template_id <> \'' . $this -> template_id . '\' AND template_group = \'' . $this -> appCore -> db -> strEscape( $this -> template_group) . '\' AND template_name = \'' . $this -> appCore -> db -> strEscape( $this -> template_name) . '\'') > 0)
				{
					$this -> setError( $this -> appCore -> lang['acp_template_name_taken']);	
				}
				else
				{
					// Add to made changes
					$this -> changes['template_name'] = $this -> appCore -> db -> strEscape( $this -> template_name);
				}
			}
		}
		else
		{
			$this -> setError( $this -> appCore -> lang['acp_template_name_empty']);	
		}
		
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Set template_group
	 *
	 * @param string $new_value
	 */
	
	function setTemplateCode( $new_value){
		
		// Store in object
		$this -> template_code = $new_value;
		
		// Add to made changes
		$this -> changes['template_code'] = $this -> appCore -> db -> strEscape( $this -> template_code);
		
	}
		
}